package artxun.commenadapterdr;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {
    ListView mListView;
    List<DomainTest> mData = new ArrayList<>();
    MultiTypeSurpport<DomainTest> mMultiType = new MultiTypeSurpport<DomainTest>() {
        @Override
        public int getViewTypeCount() {
            return 2;
        }

        @Override
        public int getLayoutId(int position, DomainTest item) {
            if(getItemViewType(position, item) == 0){
                return R.layout.item_layout;
            }
            return R.layout.item_layout2;
        }

        @Override
        public int getItemViewType(int postion, DomainTest item) {
            if(postion%2 == 0){
                return 0;
            }
            return 1;
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);
        mListView = (ListView) findViewById(R.id.lv);
        for(int i = 0; i<100;i++){
            DomainTest item = new DomainTest();
            item.setName("name"+String.valueOf(i));
            item.setContent("content" + String.valueOf(i));
            mData.add(item);
        }
        /**
         * 普通的适配器
         */
//        mListView.setAdapter(new CommonAdapter<DomainTest>(this, mData, R.layout.item_layout) {
//            @Override
//            void convert(ViewHolder vh, DomainTest item) {
//                TextView tv_name = vh.getItemView(R.id.tv_item_name);
//                TextView tv_content = vh.getItemView(R.id.tv_item_content);
//                tv_name.setText(item.getName());
//                tv_content.setText(item.getContent());
//            }
//        });
        /**
         * 多type支持的适配器
         */
        mListView.setAdapter(new CommonAdapter<DomainTest>(this, mData,mMultiType) {
            @Override
            void convert(ViewHolder vh, DomainTest item) {
                switch (vh.mLayoutId){
                    case R.layout.item_layout:
                        TextView nameTv = vh.getItemView(R.id.tv_item_name);
                        TextView contentTv = vh.getItemView(R.id.tv_item_content);
                        nameTv.setText(item.getName());
                        contentTv.setText(item.getContent());
                        break;
                    case R.layout.item_layout2:
                        TextView typeTv = vh.getItemView(R.id.tv_type);
                        typeTv.setText(item.getName());
                        break;
                }
            }
        });
    }

}
